<?php
namespace app\business\controller;
use app\business\controller\Base;
use app\business\model\BusinessEnvironment;
use app\business\model\BusinessInfo;
use app\business\model\BusinessLogin;
use think\Db;
use think\Session;

class Info extends Base
{
    public function index()
    {
      //var_dump(request()->isPost);die;
        if(request()->isPost()){

          //有提交数据
           $file = request()->file("business_logo");

           if($file){  //如果有上传图片
                $info = $file->move(ROOT_PATH . 'public/uploads/shop');
               $nowDate=date('Ymd');
                if($info){
                   $path=$nowDate.'/'.$info->getFilename();
               }else{
                   // 上传失败获取错误信息
                    echo $file->getError();
               }
               $username = Session::get('username');

               $count = BusinessLogin::where('username',$username)->find();

               $num=$count['business_id'];

                $_POST['business_logo']=$path;

                $circle = $_POST['business_circle'];

                $circle_name = Db::table('business_circle')->where('id',$circle)->find();

                $_POST['business_circle'] = $circle_name;
                //var_dump($_POST);die;
                Db::table('business_info')->where('business_id', $num)->update($_POST);


           }else{
               $username = Session::get('username');

               $count = BusinessLogin::where('username',$username)->find();

               $num=$count['business_id'];      //当前商铺的id

               $info=BusinessInfo::where('business_id',$num)->find();

               $business_logo = $info['business_logo'];

               $_POST['business_logo'] = $business_logo;

               $circle = $_POST['business_circle'];

               $circle_name = Db::table('business_circle')->where('id',$circle)->find();

               $_POST['business_circle'] = $circle_name['circle_name'];

               Db::table('business_info')->where('business_id', $num)->update($_POST);
           }
//                $id=$_POST['business_id'];
//                unset($_POST['business_id']);
//                $demo=Db::table('business_info')->where('business_id', $id)->find($id);
//                $_POST['business_img']=$demo['business_img'];
//                Db::table('business_info')->where('business_id', $id)->update($_POST);
//            }
        }
        //下拉框的商圈


        $obj = Session::get('business_id');
        $num = BusinessLogin::where('business_id',$obj)->find();
        $data = BusinessInfo::where('business_id',$num['business_id'])->find();
        $select = Db::table('business_circle')->where('agent_id',$data['agent_id'])->select();

        $this->assign('select',$select);
        $select = Db::table('business_circle')->where('circle_name',$data['business_circle'])->find();
        $data['business_circleId'] = $select['id'];
        $environment = BusinessEnvironment::where('business_id',$num['business_id'])->select();

        //var_dump($data);die;
        $this->assign('data',$data);
        $this->assign('environment',$environment);
        return $this->fetch();
    }

    public function upload()
    {
        $file = request()->file("file");

        $info = $file->move(ROOT_PATH . 'public/uploads/shop');

        $nowDate=date('Ymd');

        $path=$nowDate.'/'.$info->getFilename();

        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //这个是当前商家

        //添加数据
        $data = ['business_id' => $num, 'environment_img' => $path,'business_sort' => 0 ];

        Db::table('business_environment')->insert($data);

        return true;
    }

    public function deleteEnvironment()
    {
        $num=$_POST['data'];
        BusinessEnvironment::where('id',$num)->delete();
        return true;
    }
//修改公众号图片
    public function gzhupdate()
    {
      return $this->fetch();
    }

    public function gzhdo_update()
    {
      $where['gzhimg'] = $this->uploads_img();
      $where['id'] = $_SESSION['think']['business_id'];

      db('info')->update($where);

      return $this->success('保存成功！');
    }

    public function uploads_img()
    {


        $file = request()->file("gzhimg");
 //var_dump($file);die;
        if (!$file) {
          $this->error('请重新选择图片！');
        }
        $info = $file->move(ROOT_PATH . 'public/uploads/shop');

        $nowDate=date('Ymd');

        $path=$nowDate.'/'.$info->getFilename();

        return $path;
    }

}